﻿using System;
using System.Reflection;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

namespace Dotsa.FAQ
{
    /// <summary>
    /// ToolPart allowing selection of a list on the current site via a drop down list.
    /// </summary>
    public class GenericListSelectorToolPart : ToolPart
    {
        private string _ListFieldName;
        public string _ListFieldGuid;
        private Control _parentUserControl;
        private Guid _ListFeatureId = Guid.Empty;
        /// <summary>
        /// Initializes a new instance of the <see cref="GenericListSelectorToolPart"/> class.
        /// </summary>
        private GenericListSelectorToolPart()
            : base()
        {
            _ListFieldName = "ListName";
            _ListFieldGuid = "ListGuid";
        }

        public GenericListSelectorToolPart(Control parentUserControl)
            : this(parentUserControl, "ListName")
        {
            _ListFieldGuid = "ListGuid";
        }

        public GenericListSelectorToolPart(Control parentUserControl, string strListFieldName)
            : this(parentUserControl, strListFieldName, Guid.Empty)
        {
        }

        public GenericListSelectorToolPart(Control parentUserControl, string strListFieldName, Guid listFeatureId)
            : base()
        {
            _ListFieldName = strListFieldName;
            _parentUserControl = parentUserControl;
            _ListFeatureId = listFeatureId;
            _ListFieldGuid = "ListGuid";
        }

        public GenericListSelectorToolPart(string strListFieldName)
            : base()
        {
            _ListFieldName = strListFieldName;
        }

        /// <summary>
        /// Sends the tool part content to the specified HtmlTextWriter object, which writes the content to be rendered on the client.
        /// </summary>
        /// <param name="output">The HtmlTextWriter object that receives the tool part content.</param>
        protected override void RenderToolPart(HtmlTextWriter output)
        {
            //Type webPartType = typeof(UserControl);
            string strListName = string.Empty;
            string strListGuid = string.Empty;
            try
            {
                PropertyInfo info = _parentUserControl.GetType().GetProperty(_ListFieldName);
                strListName = (string)info.GetValue(_parentUserControl, null);

                PropertyInfo listGuid = _parentUserControl.GetType().GetProperty(_ListFieldGuid);
                strListGuid = (string)listGuid.GetValue(_parentUserControl, null);
            }
            catch (Exception ex)
            {
                output.Write(string.Format("Your Web Part doesn't contain a property. {0}", ex.ToString()));
                return;
            }

            output.Write("<table cellspacing=\"0\" border=\"0\" style=\"border-width:0px;width:100%;border-collapse:collapse;\">");
            output.Write("<tr><td>");
            output.Write("<div class=\"UserControlGroup\">");
            output.Write("<select name=\"picker_list\" style=\"width:100%\" id=\"picker_list\">");

            foreach (SPList l in SPContext.Current.Web.Lists)
            {
                bool displayList = false;
                if (!l.Hidden)
                {
                    if (_ListFeatureId != Guid.Empty)
                    {
                        if (l.TemplateFeatureId == _ListFeatureId)
                        {
                            displayList = true;
                        }
                    }
                    else
                    {
                        displayList = true;
                    }
                    if (displayList)
                    {
                        output.Write("<option value=\"" + l.Title + "\"");
                        if (l.Title == strListName) output.Write(" selected=\"true\"");
                        output.Write(">");
                        output.Write(l.Title);
                        output.Write("</option>");
                    }

                }
            }

            output.Write("</select><br/>");
            output.Write("</div>");
            output.Write("</td></tr>");
            output.Write("<tr><td>");
            output.Write("List Guid : <br/>");
            output.Write("<input type=\"text\" name=\"list_GUID\" value=\"" + strListGuid + "\" />");
            output.Write("</td></tr>");
            output.Write("</table>");

        }

        /// <summary>
        /// Called when the user clicks the OK or the Apply button in the tool pane.
        /// </summary>
        public override void ApplyChanges()
        {
            try
            {
                PropertyInfo info = _parentUserControl.GetType().GetProperty(_ListFieldName);
                info.SetValue(_parentUserControl, Page.Request.Form["picker_list"], null);

                PropertyInfo listGuid = _parentUserControl.GetType().GetProperty(_ListFieldGuid);
                listGuid.SetValue(_parentUserControl, Page.Request.Form["list_GUID"], null);
            }
            catch (Exception ex)
            {
                string strError = ex.ToString();
            }
        }
    }
}
